package handlers

import (
	"github.com/gin-gonic/gin"
	"pic-text-share/models"
	"pic-text-share/pkg"
)

func Register(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "账号和密码不能为空",
		})
		return
	}
	data := models.User{
		Username: username,
		Password: pkg.Md5(password),
	}
	var user models.User
	models.DB.Where("username=?", username).Find(&user).Debug()
	if user.ID > 0 {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "用户已注册",
		})
		return
	}
	models.DB.Create(&data).Debug()
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "注册成功",
	})
	return
}
